package com.hzpd.hainan.app;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.custorm.CustomProgressDialog;
import com.hzpd.hainan.utils.SPUtil;

import butterknife.ButterKnife;

public abstract class ToolBarActivity extends BaseActivity {
    TextView tvTitle;
    TextView tv_change;
    Toolbar toolbar;
    ImageView search_tool_bar;
    protected SPUtil spu;//
    protected Activity activity;
    protected FragmentManager fm;
    private CustomProgressDialog dialog;
    private ImageView img_title_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme_NoActionBar);
        setContentView(R.layout.activity_tool_bar);
        activity=this;
        fm=getSupportFragmentManager();
        spu =SPUtil.getInstance();
        initToolBar();
        initContentView();
        initView();
        initData();

    }
    /**
     * 初始化toolbar
     */
    private void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tv_change = (TextView) findViewById(R.id.tv_change);
        img_title_id = (ImageView) findViewById(R.id.img_title_id);
        search_tool_bar=(ImageView)findViewById(R.id.search_tool_bar);
        toolbar.setPopupTheme(R.style.AppTheme_PopupOverlay);
        toolbar.setSubtitleTextColor(Color.WHITE);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.nav_more));
        toolbar.setTitle("");
        tvTitle.setText(null == initToolBarTitle() ? getString(R.string.app_name) : initToolBarTitle());
        setSupportActionBar(toolbar);
        showBackButton();

    }
    public void setToolBarVisiable(boolean isVisiable) {
        if (isVisiable) {
            toolbar.setVisibility(View.VISIBLE);
            return;
        }
        toolbar.setVisibility(View.GONE);
    }
    public void setMainTitle(){
        tvTitle.setVisibility(View.GONE);
        img_title_id.setVisibility(View.VISIBLE);
    }
    public void setLogo(boolean iszhongwen){
        if(iszhongwen){
            img_title_id.setImageDrawable(getResources().getDrawable(R.drawable.title_logo));
        }else{
            img_title_id.setImageDrawable(getResources().getDrawable(R.drawable.logo_zang));
        }
    }
    protected void hiteToolBarBack(){
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }
    /**
     * 返回按钮显示
     */
    public void showBackButton() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.mipmap.back_icon);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

    }
    public void hideBackButton(){
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

    /**
     * 设置布局
     */
    private  void initContentView() {
        FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_layout);
        getLayoutInflater().inflate(setMyContentView(), contentLayout);
        ButterKnife.bind(this);
    }
    public void showDialog() {
        if (dialog == null)
            dialog = CustomProgressDialog.createDialog(activity, false);
        dialog.show();
    }

    public void disMissDialog(){
        if (dialog != null){
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
    public abstract String initToolBarTitle();//设置title
    public abstract int setMyContentView();//继承类的布局

    public void initView() {}//规范控件初始化

    public void initData() {}//规范数据初始化
    @Override
    protected void onDestroy() {

        if (dialog!=null){
            dialog.dismiss();
        }super.onDestroy();
    }
}
